﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Movies
{
    class HelperNameUtil
    {

        private static int startYear = 1900;
        private static int endYear = DateTime.Now.Year;
        private static String spaceDashDotUnderscoreNText = "bluray,blueray,720p,X264,BPRip,BRRip,BDRip,DVDRip,HDCAM,CAM,HD,3D,DTS,XVID,HDTV,UNRATED,.avi,.mpg,.dat,.vob,.mkv,.mp4,_details";
        private static List<String> delimiter;


        public static double GetDegreeMatch(String input1, String input2)
        {

            if (input1 != null && input2 != null && !input1.Trim().Equals("") && !input2.Trim().Equals(""))
            {
                input1 = input1.Trim().ToLower();
                input2 = input2.Trim().ToLower();

                if (input1.ToLower().Equals(input2.ToLower()))
                {
                    return 100;
                }

                char[] charSplit = { ' ' };
                String[] stringArrayA = input1.Split(charSplit);
                String[] stringArrayB = input2.Split(charSplit);

                double strLen1 = stringArrayA.Length;
                double strLen2 = stringArrayB.Length;
                String[] baseArray = stringArrayB;
                String[] compArray = stringArrayA;
                if (strLen1 >= strLen2)
                {
                    baseArray = stringArrayA;
                    compArray = stringArrayB;
                }

                double diffComponent = ((double)compArray.Length) / baseArray.Length;
                int countForMatch = 1;
                int lastMatchLocation = 0;
                int firstNotFoundLocation=0;
                int foundMatch = 0;
                for (int compIndex = 0; compIndex < compArray.Length; compIndex++)
                {
                    if (firstNotFoundLocation!=0 && firstNotFoundLocation < lastMatchLocation)
                    {
                        lastMatchLocation = firstNotFoundLocation;
                        firstNotFoundLocation = 0;
                    }
                    for (int baseIndex = lastMatchLocation+1; baseIndex < baseArray.Length; baseIndex++)
                    {
                        countForMatch++;
                        if (GetMatch(compArray[compIndex],baseArray[baseIndex]))
                        {
                            foundMatch++;
                            lastMatchLocation = baseIndex;
                            break;
                        }else if(firstNotFoundLocation==0){
                            firstNotFoundLocation = baseIndex;
                        }

                        //lastMatchLocation = 0;
                    }
                    
                }
                if (countForMatch <= 0)
                {
                    countForMatch = 1;
                }

                double matchComponent = ((double)( foundMatch))/countForMatch;
                double matchPercentage = diffComponent * matchComponent;

                return (matchPercentage*100);
            }
            return 0.0;
        }

        private static bool GetMatch(String str1, String str2)
        {
            str1=str1.Trim();
            str2=str2.Trim();
            List<string> str1Variation = new List<string>();

            str1Variation.Add(str1);
            str1Variation =  buildMatchList(str1, str1Variation);

            foreach (String comp in str1Variation)
            {
                if (str2.Equals(comp))
                {
                    return true;
                }
            }
            return false;
        }

        private static List<string> buildMatchList(string input,List<string> inputList)
        {

            if (input.IndexOf("ee")>-1)
            {
                inputList.Add(input.Replace("ee", "i"));
            }
            if (input.IndexOf("i") > -1)
            {
                inputList.Add(input.Replace("i", "ee"));
            }
            if (input.IndexOf("a") > -1)
            {
                inputList.Add(input.Replace("a", "e"));
            }
            if (input.IndexOf("e") > -1)
            {
                inputList.Add(input.Replace("e", "a"));
            }

            if (input.IndexOf("vi") > -1)
            {
                inputList.Add(input.Replace("vi", "6"));
            }
            if (input.IndexOf("v") > -1)
            {
                inputList.Add(input.Replace("v", "5"));
            }
            if (input.IndexOf("iv") > -1)
            {
                inputList.Add(input.Replace("iv", "4"));
            }
            if (input.IndexOf("iii") > -1)
            {
                inputList.Add(input.Replace("iii", "3"));
            }
            if (input.IndexOf("ii") > -1)
            {
                inputList.Add(input.Replace("ii", "2"));
            }
            if (input.IndexOf("i") > -1)
            {
                inputList.Add(input.Replace("i", "1"));
            }

            return inputList;
        }

        public static String NormalizeMovieName(String name)
        {
            if (name == null)
            {
                return "";
            }
            
            if (name.IndexOf("\\") > -1)
            {
                name = name.Substring(name.LastIndexOf("\\") + 1);
            }

            foreach (String token in GetValidDelimiter())
            {
                int indexOf = -1;
                String lowerName = name.ToLower();
                if ((indexOf = lowerName.IndexOf(token)) > -1)
                {
                    name = name.Substring(0, indexOf);
                    lowerName = name.ToLower();
                }
            }
            name = name.Replace('_', ' ');
            name = name.Replace('-', ' ');
            name = name.Replace('.', ' ');
            name = name.Replace(':', ' ');
            name = name.Replace('?', ' ');
            name = name.Replace("  ", " ");
            name = name.Trim();

            return ToTitleCase(name);
        }

        public static String ToTitleCase(String input)
        {
            String[] inputString = input.Split(' ');
            StringBuilder returnName = new StringBuilder();
            foreach (String part in inputString)
            {
                if (part.Length > 1)
                {
                    returnName.Append(part.Substring(0, 1).ToUpper() + part.Substring(1) + " ");
                }
                else if (part.Length == 1)
                {
                    returnName.Append(part.ToUpper() + " ");
                }
            }

            return returnName.ToString().Trim();
        }


        private static List<String> GetValidDelimiter()
        {
            if (delimiter == null)
            {

                delimiter = new List<String>();
               
                for (int year = startYear; year <= endYear; year++)
                {
                    delimiter.Add("[" + year + "");
                    delimiter.Add("(" + year + "");
                    delimiter.Add(" " + year + "");
                    delimiter.Add("." + year + "");
                    delimiter.Add("_" + year + "");
                    delimiter.Add("-" + year + "");
                }
                String[] notAllowedTocken = spaceDashDotUnderscoreNText.Split(",".ToCharArray());

                foreach (String token in notAllowedTocken)
                {
                    String tokenNew = token.Trim().ToLower();
                    delimiter.Add("[" + tokenNew + "");
                    delimiter.Add("(" + tokenNew + "");
                    delimiter.Add(" " + tokenNew + "");
                    delimiter.Add("." + tokenNew + "");
                    delimiter.Add("_" + tokenNew + "");
                    delimiter.Add("-" + tokenNew + "");
                    delimiter.Add(tokenNew);
                }
            }
            return delimiter;
        }




    }
}
